package com.idea.dsl.client;

import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 模型客户端工厂
 */
@Component
public class ModelClientFactory {

    private final List<ModelClient> modelClients;
    private final Map<String, ModelClient> clientMap;

    public ModelClientFactory(List<ModelClient> modelClients) {
        this.modelClients = modelClients;
        this.clientMap = modelClients.stream()
                .collect(Collectors.toMap(ModelClient::getClientType, Function.identity()));
    }
    
    /**
     * 根据类型获取客户端
     * 
     * @param clientType 客户端类型
     * @return 模型客户端
     */
    public ModelClient getClient(String clientType) {
        ModelClient client = clientMap.get(clientType);
        if (client == null) {
            throw new RuntimeException("不支持的模型类型: " + clientType);
        }
        
        if (!client.isAvailable()) {
            throw new RuntimeException("模型客户端不可用: " + clientType);
        }
        
        return client;
    }
    
    /**
     * 获取默认可用的客户端
     * 
     * @return 模型客户端
     */
    public ModelClient getDefaultClient() {
        return modelClients.stream()
                .filter(ModelClient::isAvailable)
                .findFirst()
                .orElseThrow(() -> new RuntimeException("没有可用的模型客户端"));
    }
    
    /**
     * 获取所有可用的客户端类型
     * 
     * @return 客户端类型列表
     */
    public List<String> getAvailableClientTypes() {
        return modelClients.stream()
                .filter(ModelClient::isAvailable)
                .map(ModelClient::getClientType)
                .collect(Collectors.toList());
    }
}
